﻿' Copyright (c) 2013  Christoc.com
'  All rights reserved.
' 
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
' 
Imports DotNetNuke
Imports DotNetNuke.Entities.Modules.Actions
Imports DotNetNuke.Entities.Modules
Imports DotNetNuke.Services.Exceptions
Imports DotNetNuke.Services.Localization
Imports MVPcsGoogleSearch.Components
Imports DotNetNuke.UI.Utilities
Imports JavaScriptObjectDictionary
Imports System.String

''' <summary>
''' The View class displays the content
''' 
''' Typically your view control would be used to display content or functionality in your module.
''' 
''' View may be the only control you have in your project depending on the complexity of your module
''' 
''' Because the control inherits from MVPcsGoogleSearchModuleBase you have access to any custom properties
''' defined there, as well as properties from DNN such as PortalId, ModuleId, TabId, UserId and many more.
''' 
''' </summary>
Partial Class View
    Inherits MVPcsGoogleSearchModuleBase
    Implements IActionable

    'Private _settingsID As Integer
    'Private _GoogleSearchID As String
    'Private _hideGoogleSearchBox As Boolean
    Private _CurrentSettingId As Integer
    Private _GoogleSearchID As String

    Public Property CurrentSettingId As Integer
        Get
            Return _CurrentSettingId
        End Get
        Set(value As Integer)
            _CurrentSettingId = value
        End Set
    End Property

    Public Property GoogleSearchID As String
        Get
            Return _GoogleSearchID
        End Get
        Set(value As String)
            _GoogleSearchID = value
        End Set
    End Property

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Registers the module actions required for interfacing with the portal framework
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    ''' <history>
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public ReadOnly Property ModuleActions() As ModuleActionCollection Implements IActionable.ModuleActions
        Get
            Dim Actions As New ModuleActionCollection
            Actions.Add(GetNextActionID, Localization.GetString("EditModule", LocalResourceFile), Entities.Modules.Actions.ModuleActionType.AddContent, "", "", EditUrl(), False, DotNetNuke.Security.SecurityAccessLevel.Edit, True, False)
            Return Actions
        End Get
    End Property

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Page_Load runs when the control is loaded
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Try

            If Not IsPostBack Then
                Dim tc As New GooglSearchSettingController()
                If tc.GetGooglSearchSettings(ModuleId).Count > 0 Then
                    CurrentSettingId = tc.GetGooglSearchSettings(ModuleId).First.SettingId
                    Dim s As New GooglSearchSetting
                    s = tc.GetGooglSearchSetting(CurrentSettingId, ModuleId)
                    GoogleSearchID = s.GoogleSearchID
                    Load_Script()
                    If s.HideSearchBox Then
                        GSearchBox.Visible = False
                    Else
                        GSearchBox.Visible = True
                    End If

                    GSearchResults.Visible = True
                    lblEmptyMessage.Visible = False
                Else
                    GSearchBox.Visible = False
                    GSearchResults.Visible = False
                    lblEmptyMessage.Visible = True
                End If

            End If
        Catch exc As Exception
            Exceptions.ProcessModuleLoadException(Me, exc)
        End Try

    End Sub



    Public Sub Load_Script()
        If (Not GoogleSearchID = "" Or GoogleSearchID IsNot Nothing) Then



            Dim cstype As Type = Me.GetType()

            Dim cs As ClientScriptManager = Page.ClientScript

            Dim script As New StringBuilder()
            script.Append("<script>")
            script.Append("(function () {")
            script.Append("var cx = '" + GoogleSearchID + "';")
            script.Append("var gcse = document.createElement('script');")
            script.Append("gcse.type = 'text/javascript';")
            script.Append("gcse.async = true;")
            script.Append("gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +")
            script.Append("'//www.google.com/cse/cse.js?cx=' + cx;")
            script.Append("var s = document.getElementsByTagName('script')[0];")
            script.Append("s.parentNode.insertBefore(gcse, s);")
            script.Append("})();")
            script.Append("</script>")

            cs.RegisterClientScriptBlock(cstype, "", script.ToString(), False)
        End If
    End Sub
  
End Class